package com.ly.hotel.view.fragments.vod;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.Vodvideo;
import com.ly.hotel.util.ImageUtil;
import com.ly.hotel.util.LanguageUtil;

import java.util.List;

public class VodItemAdapter extends BaseAdapter {
	private Context mContext;
	private List<Vodvideo> movieList;
	private String tmp[];
	private int rod;
	LayoutInflater mInflater;


	public VodItemAdapter(VodActivity c, final List<Vodvideo> movieInfos) {
		movieList = movieInfos;
		mContext = c;
		rod = mContext.getResources().getDimensionPixelSize(R.dimen.px9);
		mInflater = LayoutInflater.from(mContext);
		tmp = MyApplication.getHostIp().split(":");
	}

	@Override
	public int getCount() {
		return movieList.size();
	}

	@Override
	public Object getItem(int position) {
		return movieList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setData(List<Vodvideo> movieInfos){
		this.movieList = movieInfos;
	}

	@Override
	public View getView(int position, View contentView, ViewGroup arg2) {
		ViewHolder holder;
		if (contentView == null) {
			holder = new ViewHolder();
			contentView = mInflater.inflate(R.layout.vod_recycleview_item, null);
			holder.mImg = contentView.findViewById(R.id.mVodImage);
			holder.mTxt= contentView.findViewById(R.id.mVodtext);
			contentView.setTag(holder);
		} else {
			holder = (ViewHolder) contentView.getTag();
		}
		Vodvideo item = movieList.get(position);
		String imgPath;
		if(tmp.length == 2 && !movieList.get(position).getServerPort().equals("0")){
			imgPath = "http://" + tmp[0] + ":" + movieList.get(position).getServerPort() + "/" + movieList.get(position).getImg();
		}else{
			imgPath = MyApplication.getHostResUrl() + movieList.get(position).getImg();
		}
		ImageUtil.displayImage_Round(mContext, imgPath, holder.mImg,rod);
		holder.mTxt.setText(LanguageUtil.getTextFromLocalLanguage(item.getName()));
		return contentView;
	}

	static class ViewHolder {
		ImageView mImg;
		TextView mTxt;
	}
}
